MsgN("-- cl_spawmenu.lua loaded")

hook.Add("SpawnMenuEnabled", "MDF.SpawnMenuEnabled", function()
	return !MDF.IsEnabled()
end)

hook.Add("SpawnMenuOpen", "MDF.SpawnMenuOpen", function()
	return !MDF.IsEnabled()
end)

hook.Add("OnSpawnMenuOpen", "MDF.OnSpawnMenuOpen", function()
	if (!MDF.IsEnabled()) then return end
	if (!MDFMenu) or (!MDFMenu:IsValid()) then MDFMenu = vgui.Create("MDFMenu") end

	MDFMenu:Open()
end)

hook.Add("OnSpawnMenuClose", "MDF.OnSpawnMenuClose", function()
	if (!MDF.IsEnabled()) or (!MDFMenu) or (!MDFMenu:IsValid()) then return end
	MDFMenu:Close()
end)






--[[

	Main Spawn Menu

]]
local PANEL = {}

function PANEL:Init()
	MDFMenu = self

	self:SetTitle("")
	self:ShowCloseButton(false)
	self:SetSize(ScrW() - 50, ScrH() - 50)
	self:SetPos(25, 25)
	self.Paint = function()
		surface.SetDrawColor(0, 0, 0, 0)
		surface.DrawRect(0, 0, self:GetWide(), self:GetTall())
	end

	self.LeftPanel = vgui.Create( "DPropertySheet", self )
	self.LeftPanel:SetSize((self:GetWide()*.5 - 5), self:GetTall())
	self.LeftPanel:SetPos(0, 0)

	self.RightPanel = vgui.Create( "DPropertySheet", self )
	self.RightPanel:SetSize((self:GetWide()*.5 - 5), self:GetTall())
	self.RightPanel:SetPos((self:GetWide()*.5 + 5), 0)

	self.LeftPanel:AddSheet("Props", vgui.Create( "mdf_props", self.LeftPanel ), "gui/silkicons/brick_add", true, false)
	self.LeftPanel:AddSheet("Weapons", vgui.Create( "mdf_weapons", self.LeftPanel ), "gui/silkicons/bomb", true, false)
	self.RightPanel:AddSheet("Tools", vgui.Create( "mdf_tools", self.RightPanel ), "gui/silkicons/wrench", false, false)
	self.RightPanel:AddSheet("Help & Rules", vgui.Create( "mdf_help", self.RightPanel ), "gui/silkicons/exclamation", false, false)
end

function PANEL:Open()
	self:SetVisible( true )

	gui.EnableScreenClicker(true)
	RestoreCursorPosition()
end

function PANEL:Close()
	self.BaseClass.Close( self )

	RememberCursorPosition()
	gui.EnableScreenClicker(false)
end

vgui.Register("MDFMenu", PANEL, "DFrame")



--[[

	Props Panel

]]
local PANEL = {}

function PANEL:Init()

	self:EnableHorizontal( true )
	self:SetPadding(5)
	self:SetSpacing(5)
	self:SetSize((self:GetParent():GetWide() - 10), self:GetParent():GetTall())
	self:SetPos(0, 0)

	for propModel, propInfo in pairs(list.Get("FloodProps")) do

		local prop = vgui.Create("SpawnIcon")
		prop:SetModel(propModel)
		prop:SetToolTip("Health: " .. tostring(propInfo.health) .. "\nCost: " .. tostring(propInfo.cost))

		prop.DoClick = function()
			RunConsoleCommand("MDFSpawnProp", propModel)
			--MDFMenu:Close()
		end

		self:AddItem(prop)
	end
end

vgui.Register( "mdf_props", PANEL, "DPanelList" )



--[[

	Weapons Panel

]]
local PANEL = {}

function PANEL:Init()
	self:EnableHorizontal( true )
	self:EnableVerticalScrollbar( false )
	self:SetSize((self:GetParent():GetWide() - 10), self:GetParent():GetTall())
	self:SetPos(0, 0)

	for weaponModel, weaponInfo in pairs(list.Get("FloodWeapons")) do

		local swep = vgui.Create("SpawnIcon")
		swep:SetModel(weaponModel)
		swep:SetToolTip(weaponInfo.tip)

		swep.DoClick = function()
			RunConsoleCommand("MDFSpawnWeapon", weaponModel)
			--MDFMenu:Close()
		end

		self:AddItem(swep)
	end
end

vgui.Register( "mdf_weapons", PANEL, "DPanelList" )



--[[

	Tools Panel

]]
local PANEL = {}

function PANEL:Init()
	self:EnableHorizontal( true )
	self:EnableVerticalScrollbar( false )
	self:SetAutoSize( false )
	self:SetPadding(0)
	self:SetSpacing(5)
	self:SetSize(0, 0)
	self:SetPos(0, 0)
	self.Paint = function()
		surface.SetDrawColor(170, 170, 170, 255)
		surface.DrawRect(0, 0, self:GetWide(), self:GetTall())
	end

	self.ToolList = vgui.Create("DPanelList")
	self:AddItem(self.ToolList)
	self.ToolList:EnableVerticalScrollbar(true)
	self.ToolList:SetSize(self:GetParent():GetWide()*.33, self:GetParent():GetTall() - 31)
	self.ToolList:SetPos(0, 0)
	self.ToolList:SetAutoSize( false )
	self.ToolList:SetSpacing( 1 )
	self.ToolList:SetPadding( 0 )

	self.Content = vgui.Create("DPanelList")
	self:AddItem(self.Content)
	self.Content:SetSize(self:GetParent():GetWide()*.63, self:GetParent():GetTall() - 31)
	self.Content:EnableVerticalScrollbar(false)
	self.Content:SetSpacing(0)
	self.Content:SetPadding(5)

	if (!AllTools) then AllTools = spawnmenu.GetTools() end
	
	for _, tool in pairs( AllTools[1].Items ) do
		if ( type( tool ) == "table" ) then
			local info = table.Copy(tool)
			local Name = info.ItemName
			local Label = info.Text

			info.ItemName = nil
			info.Text = nil

			self:AddCategory( Name, Label, info )
		end
	end
end

function PANEL:AddCategory( Name, Label, tItems )

	self.Category = vgui.Create( "DCollapsibleCategory")
 	self.ToolList:AddItem( self.Category )
 	self.Category:SetLabel( Label )
 	self.Category:SetCookieName( "ToolMenu."..tostring(Name) )

 	self.CategoryContent = vgui.Create( "DPanelList" )
 	self.CategoryContent:SetAutoSize( true )
 	self.CategoryContent:SetDrawBackground( false )
 	self.CategoryContent:SetSpacing( 0 )
 	self.CategoryContent:SetPadding( 0 )
 	self.Category:SetContents( self.CategoryContent )

	local bAlt = true

 	for k, v in pairs( tItems ) do
 		--check against allowed tools table
		if (table.HasValue(AllowedTools, v.ItemName)) then
	 		local Item = vgui.Create( "ToolMenuButton", self )
	 		Item:SetText( v.Text or "Unknown" )
	 		Item.OnSelect = function( button ) self:EnableControlPanel( button ) end
	 		concommand.Add( Format( "tool_%s", v.ItemName ), function() Item:OnSelect() end )

	 		if ( v.SwitchConVar ) then
	 			Item:AddCheckBox( v.SwitchConVar )
	 		end

	 		Item.ControlPanelBuildFunction = v.CPanelFunction
	 		Item.Command = v.Command
	 		Item.Name = v.ItemName
	 		Item.Controls = v.Controls
	 		Item.Text = v.Text

	 		Item:SetAlt( bAlt )
	 		bAlt = !bAlt

 			self.CategoryContent:AddItem( Item )
 		end
 	end
end

function PANEL:EnableControlPanel( button )
	if ( self.LastSelected ) then self.LastSelected:SetSelected( false ) end

	button:SetSelected( true )
	self.LastSelected = button

	local cp = controlpanel.Get( button.Name )

	if ( !cp:GetInitialized() ) then cp:FillViaTable( button ) end

	self.Content:Clear()
	self.Content:AddItem( cp )
	self.Content:Rebuild()

	g_ActiveControlPanel = cp

	if ( button.Command ) then LocalPlayer():ConCommand( button.Command ) end
end

vgui.Register( "mdf_tools", PANEL, "DPanelList" )



--[[

	Help Panel

]]
local PANEL = {}

function PANEL:Init()
	self:EnableVerticalScrollbar(false)
	self:SetSize((self:GetParent():GetWide() - 10), self:GetParent():GetTall())
	self:SetPos(0, 0)

	self.Paint = function( )
		local Wide = self:GetWide()
		local Height = self:GetTall()

		local Title = "The 6 golden rules you must follow at all times:"

		surface.SetDrawColor(50, 50, 50, 255)
		surface.DrawRect(0, 0, Wide, Height)
		surface.SetTextColor(255, 255, 255, 255)

		surface.SetFont("Trebuchet20")
		local TitleW, TitleH = surface.GetTextSize(Title)
		surface.SetTextPos(((Wide*.5) - (TitleW*.5)), ((Height*.03) - (TitleH*.5)))
		surface.DrawText(Title)
		
		local text = {}
		
		text[1] = "Do not hold up a fight round, even if no one is dead."
		text[2] = "Attatch/constraint/stick props to the map."
		text[3] = "Spam mic/chat/props in any way."
		text[4] = "Don't CONSTANTLY build sharks, you can, just dont push it."
		text[5] = "Don't be disrespectful in any wat to other players."
		text[6] = "Don't exploit any bugs/glitches/errors with the gamemode."
		text[7] = "If any of these rules are broken, you will be kicked/banned on your fault."
		

		for _,  _text in pairs( customtext or {} ) do
			surface.SetFont("Default")
			local W, H = surface.GetTextSize(_text)
			surface.SetTextPos((Wide*.5) - (W*.5), ((Height*.09) - (H*.5)))
			surface.DrawText(_text)
		end

	end
end

vgui.Register( "mdf_help", PANEL, "DPanelList" )